package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.AboutUs;
import com.ysd.entity.NewsDetails;
import com.ysd.util.DBManager;

public class AboutUsDaoImpl {
	//查询多条,使用集合
	//访问修饰符 返回值类型 方法名 (参数列表) {方法体}
	public List<AboutUs> getAboutUs(){
		//创建集合
		List<AboutUs> list = new ArrayList<AboutUs>();
		//创建连接
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {//正常
			//创建Statement对象,用来执行sql语句
			st = conn.createStatement();
			//执行sql语句
			rs = st.executeQuery("select * from aboutUs");
			//遍历结果集
			while(rs.next()) {
				int peopleId = rs.getInt("peopleId");
				String headPortrait = rs.getString("headPortrait");
				String peopleName = rs.getString("peopleName");
				String position = rs.getString("position");
				//创建对象
				AboutUs ab = new AboutUs(peopleId, headPortrait, peopleName, position);
				list.add(ab);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {//关闭流
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	
	public List<AboutUs> getAboutUsByPage(int page,int size){
		//创建集合
		List<AboutUs> list = new ArrayList<AboutUs>();
		//创建连接
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {//正常
			//创建Statement对象,用来执行sql语句
			st = conn.createStatement();
			//执行sql语句
			String sql = "select * from aboutus limit "+(page-1)*size+","+size;
			System.out.println(sql);
			rs = st.executeQuery(sql);
			//遍历结果集
			while(rs.next()) {
				int peopleId = rs.getInt("peopleId");
				String headPortrait = rs.getString("headPortrait");
				String peopleName = rs.getString("peopleName");
				String position = rs.getString("position");
				//创建对象
				AboutUs ab = new AboutUs(peopleId, headPortrait, peopleName, position);
				list.add(ab);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {//关闭流
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	
	/**
	 * 根据设计师返回的Id删除产品
	 * @param id peopleId
	 * @return 数据库受影响行数
	 */
	public int delAboutUs(int dId) {
		return DBManager.executeUpdate("delete from aboutus where peopleId="+dId);//删除sql语句
	}
	/**
	 * 根据设计师中的多字段添加数据
	 * @param ab
	 * @return 受影响行数
	 */
	public int addAboutUs(AboutUs ab) {
		return DBManager.executeUpdate("insert into aboutus values(null,'"+ab.getHeadPortrait()+"','"+ab.getPeopleName()+"','"+ab.getPosition()+"')");
	}
	
	public List<AboutUs> getAboutByPeoName(String name){
		List<AboutUs> abList = new ArrayList<AboutUs>();
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from aboutus where peopleName='"+name+"'"); 
					
			while(rs.next()) {
				int peopleId = rs.getInt("peopleId");
				String headPortrait = rs.getString("headPortrait");
				String peopleName = rs.getString("peopleName");
				String position = rs.getString("position");
				AboutUs us = new AboutUs(peopleId, headPortrait, peopleName, position);
				abList.add(us);
			}
			return abList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 根据id查询产品
	 * @param abId
	 * @return
	 */
	public AboutUs getAboutUsById(int abId){
		//创建连接
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		AboutUs ab = null;
		
		try {//正常
			//创建Statement对象,用来执行sql语句
			st = conn.createStatement();
			//执行sql语句
			rs = st.executeQuery("select * from aboutus where peopleId="+abId);
			//遍历结果集
			while(rs.next()) {
				int peopleId = rs.getInt("peopleId");
				String headPortrait = rs.getString("headPortrait");
				String peopleName = rs.getString("peopleName");
				String position = rs.getString("position");
				//创建对象
				ab = new AboutUs(peopleId, headPortrait, peopleName, position);
			}
			return ab;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {//关闭流
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 根据设计师id修改数据
	 * @param ab
	 * @return
	 */
	public int updateAboutUs(AboutUs ab) {
		String sql = "update aboutus set headPortrait='"+ab.getHeadPortrait()+"',peopleName='"+ab.getPeopleName()+"',position='"+ab.getPosition()+"'  where peopleId="+ab.getPeopleId();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return 多个id为String
	 */
	public int delAboutUsByIds(String ids) {
		String sql = "delete from aboutus where peopleId in("+ids+")";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
}
